package gui.menubar;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import model.MetroModel;
import model.savers.MetroSaversRegistry;

/**
 * 
 * @author Ilya Shurigyn
 *
 */
public class SaveMapMenuItem extends JMenuItem {
	
	public SaveMapMenuItem() {
		super("Save map");
		addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				showSaveFileDialog(SaveMapMenuItem.this);
				
			}
			
		});
	}
	
	public static void showSaveFileDialog(Component c) {
		JFileChooser fc = new JFileChooser();
		for(MetroSaversRegistry.FileFormat format:MetroSaversRegistry.getFormats()) {
			FileFilter ff = new FileNameExtensionFilter(format.shortFileFormatDescription, format.formatFileNameExtension);			
			fc.addChoosableFileFilter(ff);
		}
		fc.setFileFilter(fc.getChoosableFileFilters()[1]); //set default FileFilter to AndroidMap;
		
		if( fc.showSaveDialog(c) == JFileChooser.APPROVE_OPTION) {
			File f = fc.getSelectedFile();
			System.out.println(f.getName());
			String currentExtension = ((FileNameExtensionFilter)fc.getFileFilter()).getExtensions()[0];
			MetroModel.getInstance().save(f.getAbsolutePath() + "." + currentExtension);
		}

	}
}
